package cn.iocoder.yudao.module.dlbz.controller.admin.companypost;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dlbz.controller.admin.companypost.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.companypost.CompanyPostDO;
import cn.iocoder.yudao.module.dlbz.service.companypost.CompanyPostService;

@Tag(name = "管理后台 - 合作企业岗位")
@RestController
@RequestMapping("/dlbz/company-post")
@Validated
public class CompanyPostController {

    @Resource
    private CompanyPostService companyPostService;

    @PostMapping("/create")
    @Operation(summary = "创建合作企业岗位")
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:create')")
    public CommonResult<Long> createCompanyPost(@Valid @RequestBody CompanyPostSaveReqVO createReqVO) {
        return success(companyPostService.createCompanyPost(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合作企业岗位")
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:update')")
    public CommonResult<Boolean> updateCompanyPost(@Valid @RequestBody CompanyPostSaveReqVO updateReqVO) {
        companyPostService.updateCompanyPost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合作企业岗位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:delete')")
    public CommonResult<Boolean> deleteCompanyPost(@RequestParam("id") Long id) {
        companyPostService.deleteCompanyPost(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合作企业岗位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:query')")
    public CommonResult<CompanyPostRespVO> getCompanyPost(@RequestParam("id") Long id) {
        CompanyPostDO companyPost = companyPostService.getCompanyPost(id);
        return success(BeanUtils.toBean(companyPost, CompanyPostRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合作企业岗位分页")
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:query')")
    public CommonResult<PageResult<CompanyPostRespVO>> getCompanyPostPage(@Valid CompanyPostPageReqVO pageReqVO) {
        PageResult<CompanyPostDO> pageResult = companyPostService.getCompanyPostPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyPostRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合作企业岗位 Excel")
    @PreAuthorize("@ss.hasPermission('dlbz:company-post:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyPostExcel(@Valid CompanyPostPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyPostDO> list = companyPostService.getCompanyPostPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合作企业岗位.xls", "数据", CompanyPostRespVO.class,
                        BeanUtils.toBean(list, CompanyPostRespVO.class));
    }

}